﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using BEX.WebUI.Entities;
using BEX.WebUI.Models;
using BEX.Core;
using BEX.Core.Exchange;

namespace BEX.WebUI.Controllers
{
    public class ArbitrageRuleController : Controller
    {
        private BTC_SMSEntities db = new BTC_SMSEntities();

        //
        // GET: /ArbitrageRule/

        public ActionResult Index()
        {
            //var alert_rule_arbitrage = db.ALERT_RULE_ARBITRAGE.Include(a => a.CURRENCY).Include(a => a.CURRENCY1);
            List<ArbitrageRuleModel> _modelList = ArbitrageRuleModel.GetAllForUser();
            
            return View(_modelList.ToList());
        }

        //
        // GET: /ArbitrageRule/Details/5

        public ActionResult Details(int id = 0)
        {
            ALERT_RULE_ARBITRAGE alert_rule_arbitrage = db.ALERT_RULE_ARBITRAGE.Find(id);
            if (alert_rule_arbitrage == null)
            {
                return HttpNotFound();
            }
            return View(alert_rule_arbitrage);
        }

        //
        // GET: /ArbitrageRule/Create

        public ActionResult Create()
        {
            ArbitrageRuleModel _model = new ArbitrageRuleModel();

            

            return View(_model);
        }

        //
        // POST: /ArbitrageRule/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(ArbitrageRuleModel rule)
        {
            
            if (ModelState.IsValid)
            {
                rule.Save(true);
                return RedirectToAction("Index");
            }

            return View(rule);
        }

        //
        // GET: /ArbitrageRule/Edit/5

        public ActionResult Edit(int id = 0)
        {
            //ALERT_RULE_ARBITRAGE alert_rule_arbitrage = db.ALERT_RULE_ARBITRAGE.Find(id);
            ArbitrageRuleModel _model = ArbitrageRuleModel.GetByID(id);

            ViewBag.ExchangeList = GetExchangeList();

            if (_model == null)
            {
                return HttpNotFound();
            }

            return View(_model);
        }

        public SelectList GetExchangeList()
        {
            var _list = Enum.GetValues(typeof(ExchangeType)).Cast<ExchangeType>().ToArray();

            return new SelectList(_list);
        }

        //
        // POST: /ArbitrageRule/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(ArbitrageRuleModel rule)
        {
            ViewBag.ExchangeList = GetExchangeList();
            if (ModelState.IsValid)
            {
                rule.Save(false);
                return RedirectToAction("Index");
            }
            return View(rule);
        }

        //
        // GET: /ArbitrageRule/Delete/5

        public ActionResult Delete(int id = 0)
        {
            ArbitrageRuleModel _model = ArbitrageRuleModel.GetByID(id);

            if (_model == null)
            {
                return HttpNotFound();
            }
            return View(_model);
        }

        //
        // POST: /ArbitrageRule/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            //ArbitrageRuleModel _model = ArbitrageRuleModel.GetByID(id);

            ArbitrageRuleModel.Delete(id);
            
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }



                            
        public ActionResult SupportedCurrenciesList(string a, string b)
        {
            SelectList _list = ArbitrageRuleModel.GetSupportedCurrencies((ExchangeType)Enum.Parse(typeof(ExchangeType), a, true), (ExchangeType)Enum.Parse(typeof(ExchangeType), b, true));

            if (HttpContext.Request.IsAjaxRequest())
                return Json(new SelectList(
                                _list.ToArray(),
                                "Value",
                                "Text"
                                )
                           , JsonRequestBehavior.AllowGet);

            return RedirectToAction("Edit");

            

        }

        
        public ActionResult ExchangeList()
        {
            var _list = Enum.GetValues(typeof(ExchangeType)).Cast<ExchangeType>().ToArray();

            if (HttpContext.Request.IsAjaxRequest())
                return Json(new SelectList(
                                _list)
                           , JsonRequestBehavior.AllowGet);

            return RedirectToAction("Edit");
        }
    }
}